Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.5     v purrr   0.3.4
## v tibble  3.1.6     v dplyr   1.0.8
## v tidyr   1.2.0     v stringr 1.4.0
## v readr   2.1.2     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./year2.RDS")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2021-06-30"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2021-06-30"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke",
## "X7_day_ave_clarke", "Facility", "collection_num", "target",
## "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies",
## "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.25, n = 295)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 11.53688 11.57461 11.61203 11.64917 11.68600 11.72254 11.75879 11.79475
##   [9] 11.83041 11.86579 11.90087 11.93567 11.97019 12.00440 12.03830 12.07191
##  [17] 12.10522 12.13824 12.17100 12.20349 12.23572 12.26771 12.29947 12.33101
##  [25] 12.36229 12.39332 12.42406 12.45450 12.48463 12.51441 12.54385 12.57291
##  [33] 12.60158 12.62985 12.65844 12.68778 12.71738 12.74673 12.77534 12.80271
##  [41] 12.82834 12.85439 12.88271 12.91235 12.94237 12.97182 12.99975 13.02522
##  [49] 13.04727 13.06495 13.08129 13.09913 13.11726 13.13448 13.14958 13.16138
##  [57] 13.16865 13.17220 13.17366 13.17306 13.17046 13.16589 13.15938 13.15098
##  [65] 13.14072 13.12865 13.11480 13.09921 13.08193 13.06299 13.03780 13.00418
##  [73] 12.96579 12.92630 12.88937 12.85864 12.82888 12.79346 12.75401 12.71213
##  [81] 12.66945 12.62759 12.58814 12.55274 12.52300 12.49470 12.46353 12.43100
##  [89] 12.39858 12.36776 12.34004 12.31691 12.29774 12.28077 12.26577 12.25250
##  [97] 12.24073 12.23021 12.22072 12.21202 12.20386 12.19602 12.18826 12.18034
## [105] 12.17382 12.16990 12.16784 12.16687 12.16624 12.16518 12.16295 12.16010
## [113] 12.15770 12.15579 12.15439 12.15353 12.15325 12.15357 12.15453 12.15615
## [121] 12.15814 12.16025 12.16262 12.16538 12.16864 12.17254 12.17721 12.18354
## [129] 12.19207 12.20236 12.21397 12.22648 12.23944 12.25242 12.26499 12.27671
## [137] 12.28714 12.29584 12.30239 12.30718 12.31101 12.31413 12.31673 12.31905
## [145] 12.32130 12.32369 12.32409 12.32071 12.31431 12.30566 12.29551 12.28461
## [153] 12.27374 12.26364 12.25509 12.24883 12.24562 12.24624 12.25143 12.26195
## [161] 12.27572 12.29019 12.30547 12.32165 12.33884 12.35712 12.37661 12.40139
## [169] 12.43457 12.47477 12.52060 12.57067 12.62359 12.67798 12.73245 12.78561
## [177] 12.83609 12.88248 12.92341 12.95749 12.98332 13.00961 13.04406 13.08380
## [185] 13.12596 13.16766 13.20604 13.23822 13.26134 13.27252 13.27318 13.26717
## [193] 13.25525 13.23819 13.21676 13.19172 13.16385 13.13392 13.10268 13.07092
## [201] 13.03940 13.00889 12.97313 12.92757 12.87554 12.82035 12.76534 12.71382
## [209] 12.66911 12.62656 12.58003 12.53067 12.47962 12.42801 12.37700 12.32771
## [217] 12.28130 12.23890 12.19858 12.15789 12.11719 12.07682 12.03714 11.99851
## [225] 11.96128 11.92438 11.88676 11.84875 11.81068 11.77289 11.73571 11.69948
## [233] 11.66454 11.63122 11.59985 11.57077 11.54432 11.51885 11.49298 11.46742
## [241] 11.44292 11.42019 11.39997 11.38298 11.36799 11.35350 11.33988 11.32747
## [249] 11.31663 11.30772 11.30109 11.29710 11.29610 11.29716 11.29932 11.30289
## [257] 11.30817 11.31547 11.32510 11.33736 11.35391 11.37564 11.40170 11.43125
## [265] 11.46343 11.49742 11.53236 11.56741 11.60172 11.63445 11.66475 11.69178
## [273] 11.71800 11.74599 11.77514 11.80481 11.83440 11.86326 11.89077 11.91766
## [281] 11.94501 11.97269 12.00060 12.02862 12.05664 12.08455 12.11222 12.13956
## [289] 12.16635 12.19257 12.21835 12.24382 12.26912 12.29438 12.31973
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./site_objects/wrf_a_year2.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.25, n = 295)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 10.70094 10.79559 10.88826 10.97895 11.06765 11.15434 11.23902 11.32168
##   [9] 11.40231 11.48091 11.55746 11.63195 11.70438 11.77469 11.84287 11.90895
##  [17] 11.97296 12.03495 12.09494 12.15298 12.20911 12.26335 12.31542 12.36509
##  [25] 12.41244 12.45758 12.50062 12.54164 12.58076 12.61808 12.65369 12.68770
##  [33] 12.72020 12.75131 12.78034 12.80681 12.83108 12.85350 12.87444 12.89424
##  [41] 12.91327 12.92985 12.94252 12.95199 12.95895 12.96411 12.96816 12.97181
##  [49] 12.97576 12.98071 12.98455 12.98531 12.98392 12.98131 12.97838 12.97608
##  [57] 12.97532 12.97508 12.97380 12.97158 12.96856 12.96483 12.96052 12.95575
##  [65] 12.95062 12.94525 12.93976 12.93425 12.92886 12.92369 12.91985 12.91763
##  [73] 12.91588 12.91343 12.90914 12.90187 12.89298 12.88445 12.87598 12.86729
##  [81] 12.85807 12.84805 12.83693 12.82442 12.81024 12.79357 12.77437 12.75347
##  [89] 12.73174 12.71001 12.68913 12.66994 12.65184 12.63356 12.61497 12.59595
##  [97] 12.57637 12.55610 12.53502 12.51299 12.48990 12.46561 12.44000 12.41293
## [105] 12.38218 12.34676 12.30845 12.26905 12.23031 12.19403 12.16198 12.12791
## [113] 12.08614 12.03933 11.99014 11.94124 11.89529 11.85494 11.82287 11.80172
## [121] 11.78448 11.76395 11.74249 11.72250 11.70636 11.69645 11.69516 11.70282
## [129] 11.71747 11.73804 11.76346 11.79265 11.82454 11.85807 11.89216 11.92574
## [137] 11.95774 11.98709 12.01272 12.04050 12.07521 12.11411 12.15450 12.19365
## [145] 12.22883 12.25733 12.28195 12.30722 12.33302 12.35923 12.38571 12.41235
## [153] 12.43902 12.46559 12.49194 12.51794 12.54347 12.56841 12.59262 12.61598
## [161] 12.63609 12.65172 12.66451 12.67608 12.68807 12.70211 12.71984 12.74212
## [169] 12.76830 12.79768 12.82961 12.86340 12.89839 12.93389 12.96924 13.00375
## [177] 13.03676 13.06758 13.09555 13.11999 13.14023 13.15988 13.18226 13.20620
## [185] 13.23055 13.25414 13.27583 13.29444 13.30884 13.31784 13.32255 13.32488
## [193] 13.32494 13.32281 13.31859 13.31238 13.30428 13.29438 13.28277 13.26956
## [201] 13.25484 13.23871 13.22020 13.19868 13.17470 13.14884 13.12166 13.09372
## [209] 13.06559 13.03746 13.00896 12.97979 12.94967 12.91829 12.88538 12.85063
## [217] 12.81376 12.77447 12.72723 12.66937 12.60491 12.53784 12.47217 12.41192
## [225] 12.36109 12.31481 12.26597 12.21527 12.16338 12.11100 12.05879 12.00746
## [233] 11.95767 11.91013 11.86550 11.82447 11.78773 11.75445 11.72312 11.69342
## [241] 11.66501 11.63758 11.61079 11.58431 11.55825 11.53307 11.50899 11.48623
## [249] 11.46502 11.44559 11.42815 11.41292 11.40014 11.39023 11.38312 11.37831
## [257] 11.37527 11.37348 11.37243 11.37159 11.37085 11.37059 11.37106 11.37248
## [265] 11.37511 11.37916 11.38489 11.39252 11.40230 11.41446 11.42924 11.44687
## [273] 11.46699 11.48902 11.51284 11.53837 11.56551 11.59417 11.62423 11.65577
## [281] 11.68889 11.72363 11.75999 11.79799 11.83765 11.87900 11.92204 11.96680
## [289] 12.01321 12.06122 12.11084 12.16207 12.21493 12.26941 12.32553
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./site_objects/wrf_b_year2.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.25, n = 295)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 10.80014 10.86877 10.93599 11.00179 11.06613 11.12898 11.19032 11.25011
##   [9] 11.30834 11.36496 11.41996 11.47330 11.52495 11.57488 11.62311 11.66971
##  [17] 11.71478 11.75838 11.80060 11.84152 11.88121 11.91975 11.95610 11.98940
##  [25] 12.02003 12.04834 12.07471 12.09950 12.12308 12.14582 12.16807 12.19022
##  [33] 12.21261 12.23563 12.25860 12.28068 12.30205 12.32283 12.34319 12.36328
##  [41] 12.38324 12.40409 12.42637 12.44948 12.47285 12.49589 12.51802 12.53864
##  [49] 12.55719 12.57307 12.58820 12.60433 12.62056 12.63601 12.64980 12.66106
##  [57] 12.66889 12.67390 12.67733 12.67928 12.67982 12.67904 12.67704 12.67389
##  [65] 12.66969 12.66452 12.65847 12.65162 12.64407 12.63590 12.62484 12.60966
##  [73] 12.59198 12.57346 12.55573 12.54041 12.52553 12.50832 12.48926 12.46881
##  [81] 12.44745 12.42565 12.40388 12.38262 12.36233 12.33963 12.31216 12.28193
##  [89] 12.25093 12.22117 12.19466 12.17340 12.15527 12.13698 12.11872 12.10067
##  [97] 12.08304 12.06601 12.04977 12.03453 12.02048 12.00780 11.99670 11.98736
## [105] 11.98085 11.97741 11.97596 11.97544 11.97479 11.97293 11.96881 11.96521
## [113] 11.96503 11.96733 11.97119 11.97567 11.97984 11.98278 11.98356 11.98124
## [121] 11.97646 11.97078 11.96466 11.95855 11.95293 11.94825 11.94496 11.94223
## [129] 11.93895 11.93516 11.93092 11.92629 11.92131 11.91604 11.91053 11.90483
## [137] 11.89900 11.89310 11.88716 11.87848 11.86557 11.85034 11.83466 11.82045
## [145] 11.80959 11.80398 11.80110 11.79738 11.79320 11.78893 11.78493 11.78157
## [153] 11.77922 11.77824 11.77901 11.78188 11.78723 11.79542 11.80682 11.82180
## [161] 11.83999 11.86046 11.88273 11.90632 11.93076 11.95557 11.98027 12.00829
## [169] 12.04271 12.08252 12.12672 12.17431 12.22430 12.27567 12.32742 12.37857
## [177] 12.42809 12.47500 12.51830 12.55697 12.59002 12.62488 12.66784 12.71621
## [185] 12.76728 12.81835 12.86671 12.90967 12.94452 12.96855 12.98452 12.99710
## [193] 13.00645 13.01276 13.01620 13.01696 13.01520 13.01112 13.00487 12.99665
## [201] 12.98663 12.97499 12.95934 12.93814 12.91282 12.88484 12.85563 12.82664
## [209] 12.79931 12.77112 12.73907 12.70389 12.66633 12.62712 12.58700 12.54670
## [217] 12.50696 12.46852 12.42429 12.36950 12.30837 12.24511 12.18391 12.12900
## [225] 12.08458 12.04533 12.00363 11.96028 11.91606 11.87177 11.82818 11.78610
## [233] 11.74630 11.70959 11.67674 11.64855 11.62581 11.60763 11.59240 11.57980
## [241] 11.56949 11.56117 11.55451 11.54919 11.54986 11.55979 11.57657 11.59778
## [249] 11.62102 11.64386 11.66391 11.67874 11.68595 11.69269 11.70593 11.72302
## [257] 11.74129 11.75810 11.77076 11.77664 11.77625 11.77238 11.76576 11.75710
## [265] 11.74713 11.73657 11.72614 11.71656 11.70855 11.70284 11.70014 11.70117
## [273] 11.70370 11.70539 11.70681 11.70854 11.71114 11.71520 11.72127 11.72860
## [281] 11.73612 11.74402 11.75248 11.76167 11.77177 11.78296 11.79542 11.80932
## [289] 11.82490 11.84219 11.86098 11.88110 11.90236 11.92458 11.94757
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log10 SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./site_objects/wrf_c_year2.rda")

keeping in case

#save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
#save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
#save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
#save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
#save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
#save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
#save(both_ymina, file = "./plotly_objs/both_ymina.rda")
#save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

#save(both_yminb, file = "./plotly_objs/both_yminb.rda")
#save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

#save(both_yminc, file = "./plotly_objs/both_yminc.rda")
#save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")